//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package com.atjg.autocat.uvccamera.utils;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.os.Environment;
import java.io.File;
import java.io.FileOutputStream;

public class FileUtil {
    public FileUtil() {
    }

    public static boolean hasExternalStorage() {
        return Environment.getExternalStorageState().equals("mounted");
    }

    public static String getExternalStoragePath() {
        return Environment.getExternalStorageDirectory().getAbsolutePath();
    }

    public static File getSDCardDir(String foderName) {
        return !hasExternalStorage() ? null : new File(getExternalStoragePath() + File.separator + foderName);
    }

    public static File getSDCardFile(String foderName, String fileName) {
        File foder = getSDCardDir(foderName);
        if (foder == null) {
            return null;
        } else {
            return !foder.exists() && !foder.mkdirs() ? null : new File(foder, fileName);
        }
    }

    public static String getDiskCacheDir(Context context, String dirName) {
        String cachePath;
        if (("mounted".equals(Environment.getExternalStorageState()) || !Environment.isExternalStorageRemovable()) && context.getExternalCacheDir() != null) {
            cachePath = context.getExternalCacheDir().getPath();
        } else {
            cachePath = context.getCacheDir().getPath();
        }

        return cachePath + File.separator + dirName;
    }

    public static File getCacheFile(Context context, String dirName, String fileName) {
        File dirFile = new File(getDiskCacheDir(context, dirName));
        if (!dirFile.exists() && !dirFile.mkdirs()) {
            LogUtil.d("failed to create directory");
            return null;
        } else {
            return new File(dirFile.getPath() + File.separator + fileName);
        }
    }

    public static boolean deleteFile(File dirFile) {
        if (!dirFile.exists()) {
            return false;
        } else if (dirFile.isFile()) {
            return dirFile.delete();
        } else {
            File[] var1 = dirFile.listFiles();
            int var2 = var1.length;

            for(int var3 = 0; var3 < var2; ++var3) {
                File file = var1[var3];
                deleteFile(file);
            }

            return dirFile.delete();
        }
    }

    public static String saveYuv2Jpeg(File file, byte[] yuv, int width, int height) {
        return saveBitmap(file, ImageUtil.yuv2Bitmap(yuv, width, height));
    }

    public static String saveYuv2Jpeg(File file, byte[] yuv, int width, int height, float rotation) {
        return saveBitmap(file, ImageUtil.yuv2Bitmap(yuv, width, height, rotation));
    }

    public static String saveBitmap(File file, Bitmap bitmap) {
        if (file != null && bitmap != null) {
            try {
                FileOutputStream fos = new FileOutputStream(file);
                bitmap.compress(CompressFormat.JPEG, 100, fos);
                if (bitmap != null && !bitmap.isRecycled()) {
                    bitmap.recycle();
                    bitmap = null;
                }

                fos.flush();
                fos.close();
            } catch (Exception var3) {
                Exception e = var3;
                e.printStackTrace();
            }

            return file.getAbsolutePath();
        } else {
            return null;
        }
    }
}
